package com.stars.empoly.utils

import android.content.Context
import android.view.LayoutInflater
import android.view.View
import android.widget.*
import androidx.recyclerview.widget.RecyclerView
import coil.load
import com.google.gson.Gson
import com.qmuiteam.qmui.kotlin.onClick
import com.qmuiteam.qmui.layout.QMUIFrameLayout
import com.qmuiteam.qmui.layout.QMUILinearLayout
import com.qmuiteam.qmui.skin.QMUISkinValueBuilder
import com.qmuiteam.qmui.util.QMUIDisplayHelper
import com.qmuiteam.qmui.widget.QMUIRadiusImageView
import com.qmuiteam.qmui.widget.popup.QMUIPopup
import com.qmuiteam.qmui.widget.popup.QMUIPopups
import com.stars.empoly.R
import com.stars.empoly.activity.AboutActivity
import com.stars.empoly.activity.PlaceOrderActivity
import com.stars.empoly.adapter.PopuBuyAdapter
import com.stars.empoly.inte.ChoiceData
import com.stars.empoly.model.ServiceList
import com.stars.empoly.view.ExpertStarView
import com.trailer.base.utils.B
import com.trailer.base.utils.UiSwitch

/**
 * 下单-购买
 *Created by shihuiyun
 *on 2020/9/21
 */
object  PopuBuyUtils {

    fun show(context: Context, bean: ServiceList, view: View){

        var mNormalPopup = QMUIPopups.fullScreenPopup(context)
        val builder: QMUISkinValueBuilder = QMUISkinValueBuilder.acquire()
        val frameLayout = QMUIFrameLayout(context)
        //自定义布局
        val layoutInflater = context?.getSystemService(Context.LAYOUT_INFLATER_SERVICE) as LayoutInflater
        var layout = layoutInflater.inflate(R.layout.popu_buy, null)

        val ll:LinearLayout=layout.findViewById(R.id.ll)
        val imgAvater: QMUIRadiusImageView =layout.findViewById(R.id.img_avater)
        val tvName:TextView=layout.findViewById(R.id.tv_name)
        val tvYan:TextView=layout.findViewById(R.id.tv_yan)
        val tvContent:TextView=layout.findViewById(R.id.tv_content)
        val starView: ExpertStarView =layout.findViewById(R.id.starView)
        val recyclerView: RecyclerView =layout.findViewById(R.id.recycler_view)
        val tvMoney:TextView=layout.findViewById(R.id.tv_money)
        val llSubmit: QMUILinearLayout =layout.findViewById(R.id.ll_submit)
        //设置数据
        imgAvater.load(bean.technicianInfo.headImage)
        tvName.text = bean.technicianInfo.realName
        tvYan.text="工作经验："+bean.technicianInfo.workTime
        tvContent.text=bean.technicianInfo.introduce
        starView.setScore(bean.technicianInfo.stars.toFloat())
        //设置列表
        for(item in bean.content){
            item.choseNumber="0"
        }
        var adapter= PopuBuyAdapter(object :ChoiceData{
            override fun onRestuse(id: String?) {
               //计算总额
                var list=bean.content
                var money:Double=0.0
                for(item in list){
                    money += item.price.toDouble() * item.choseNumber.toInt()
                }

                tvMoney.text = ""+money

            }

        })
        recyclerView.adapter=adapter
        adapter.addData(bean.content)


        llSubmit.onClick {

            var list=adapter.data
            var choseData=ArrayList<ServiceList.ContentBean>()
            for(item in list){
                if(item.choseNumber!="0"){
                    choseData.add(item)
                }
            }

            if(choseData.size==0){

                Toast.makeText(context,"请选择相关服务",Toast.LENGTH_SHORT).show()
                return@onClick
            }

            UiSwitch.bundle(context, PlaceOrderActivity::class.java, B().putString("id",bean.technicianInfo.technicianId)
                .putString("name",bean.technicianInfo.realName)
                .putString("list", Gson().toJson(choseData)).ok())
            mNormalPopup.dismiss()

        }




        ll.onClick {
            mNormalPopup.dismiss()
        }


        builder.release();

        val size = QMUIDisplayHelper.getScreenWidth(context)
        val height = QMUIDisplayHelper.getScreenHeight(context)


        val lp: FrameLayout.LayoutParams = FrameLayout.LayoutParams(size,
                height)
        frameLayout.addView(layout, lp)


        mNormalPopup.addView(frameLayout)
        mNormalPopup.onDismiss() {
        }

        mNormalPopup.dismissIfOutsideTouch(false)
        mNormalPopup.animStyle(QMUIPopup.ANIM_GROW_FROM_CENTER)
        mNormalPopup.show(view)


    }




}
